// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    value: '',
    active: 0,
    show: true,
    todolist: [],
    list: []
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    // 存储数据
    try {
      var value = wx.getStorageSync('todolist')
      if (value) {
        // value= [{
        //   id: '1',
        //   text: 'asdasdadadsas',
        //   status: false
        // },{
        //   id: '2',
        //   text: 'asdasdadadsas',
        //   status: true
        // }]
        this.setData({todolist: value, list: value})
      } else {
        wx.setStorageSync('todolist', [])
      }
    } catch (e) { 
      wx.setStorageSync('todolist', [])
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  handleClick(e) {
    // console.log(e.currentTarget.dataset.item)
    let item = e.currentTarget.dataset.item
    let list = this.data.todolist.find(val => val.id == item.id)
    list.status = !list.status
    this.setData({todolist: this.data.todolist})
    this.updatelist()
    // let data = item.id
  },
  onChange(event) {
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
    this.setData({active: event.detail.name})
    this.updatelist()
  },
  updatelist() {
    let arr = this.data.todolist
    let key = Number(this.data.active)
    switch (key) {
      case 1:
        arr = this.data.todolist.filter(item => !item.status)
        break;
        case 2:
        arr = this.data.todolist.filter(item => item.status)
        break;
    }
    wx.setStorageSync('todolist', this.data.todolist)
    this.setData({list: arr})
  },
  sumbit() {
    let a = this.data.value
    if(a === '') {
      wx.showToast({
        title: `请输入备忘信息！`,
        icon: 'none',
      });
      return 
    }
    let date = new Date()
    this.data.todolist.push({ id: date.getTime(), text: a, status: false})
    this.setData({todolist: this.data.todolist})
    this.updatelist()
    e.stopPropagation()
    console.log(a)
  },

  handleDel(e) {
    let item = e.currentTarget.dataset.item
    let index = this.data.todolist.findIndex(val => val.id == item.id)
    this.data.todolist.splice(index, 1)
    this.setData({todolist: this.data.todolist})
    this.updatelist()
    e.stopPropagation()
  }
})
